package gui;


import java.awt.event.MouseMotionAdapter;
	import java.awt.event.MouseEvent;
	import java.awt.Color;
	import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.event.MouseAdapter;
import java.awt.*;
import javax.swing.*;

	public class DrawNDrop extends JFrame {


		private static final long serialVersionUID = 1L;
		
		private static final int TAMANO_BLOQUE = 50;
		private int _PosDrawNDropX;
		private int _PosDrawNDropY;
		private JLabel _LabelShadow;

		/*
		 public static void main(String[] args) {
		 
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						DrawNDrop frame = new DrawNDrop();
						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		*/
		public DrawNDrop() {
			
			JLayeredPane panel = new JLayeredPane();
			getContentPane().add(panel, BorderLayout.NORTH);
			
			JLabel labelMove = new JLabel("");

			labelMove.setBackground(new Color(102, 0, 153));
			
			labelMove.setIcon(new ImageIcon(DrawNDrop.class.getResource("/javax/swing/plaf/metal/icons/Error.gif")));
			
			_LabelShadow = new JLabel("");
			_LabelShadow.setIcon(new ImageIcon("D:\\workspace\\dnd\\src\\shadow.gif"));
			_LabelShadow.setBackground(new Color(102, 0, 153));
			GroupLayout gl_panel = new GroupLayout(panel);
			gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_panel.createSequentialGroup()
						.addComponent(labelMove, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
						.addComponent(_LabelShadow, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
						.addGap(47)
						.addContainerGap(287, Short.MAX_VALUE))
			);
			gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_panel.createSequentialGroup()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_panel.createSequentialGroup()
								.addGap(22)
								
							.addComponent(_LabelShadow, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
							.addComponent(labelMove, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE))
						.addContainerGap(244, Short.MAX_VALUE))
			);
			panel.setLayout(gl_panel);
			
			
			labelMove.addMouseMotionListener(new MouseMotionAdapter() {
				@Override
				   public void mouseDragged( MouseEvent evento ){
					javax.swing.JLabel labelTemp = (JLabel)evento.getSource();
					_LabelShadow.setLocation(  ((evento.getX()+labelTemp.getX())/TAMANO_BLOQUE) * TAMANO_BLOQUE, 
							((evento.getY()+labelTemp.getY())/TAMANO_BLOQUE) * TAMANO_BLOQUE);
					int posX = evento.getX() + labelTemp.getX() - _PosDrawNDropX;
					int posY = evento.getY() + labelTemp.getY() - _PosDrawNDropY;
					labelTemp.setLocation(posX, posY);
					
					getContentPane().repaint();
				   }
			});
			
			labelMove.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent evento) {
					_PosDrawNDropX = (evento.getX()-(evento.getX()/TAMANO_BLOQUE)*TAMANO_BLOQUE);
					_PosDrawNDropY = (evento.getY()-(evento.getY()/TAMANO_BLOQUE)*TAMANO_BLOQUE);
				}
				
				@Override
				public void mouseReleased(MouseEvent evento) {
					javax.swing.JLabel labelTemp = (JLabel)evento.getSource();
					labelTemp.setLocation(  ((evento.getX()+labelTemp.getX())/TAMANO_BLOQUE) * TAMANO_BLOQUE, 
											((evento.getY()+labelTemp.getY())/TAMANO_BLOQUE) * TAMANO_BLOQUE);
					getContentPane().repaint();
				}
			});
			
			
			
			
		}
	}
